package com.mars.web.common.support;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Spring MVC 控制器超类
 * 
 * <p>标题: SupperController</p>
 * <p>描述: </p>
 * <p>公司: www.yhaoquan.com</p> 
 * @作者： 杨浩泉
 * @日期：2016年11月18日
 * @版本：1.0
 */
public class SupperController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

	/** 视图前缀 */
	protected String viewPrefix;

	public SupperController() {
		setViewPrefix(defaultViewPrefix());
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResultAddOk() {
		return setResponseResult(null, 200, true, "添加成功", null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultAddFail() {
		return setResponseResult(null, 200, false, "添加失败", null, null) ;
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResultDeleteOk() {
		return setResponseResult(null, 200, true, "删除成功", null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultDeleteFail() {
		return setResponseResult(null, 200, false, "删除失败", null, null) ;
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResultUpdateOk() {
		return setResponseResult(null, 200, true, "修改成功", null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultUpdateFail() {
		return setResponseResult(null, 200, false, "修改失败", null, null) ;
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResultFailed(String msg) {
		return setResponseResult(null, 200, false, msg, null, null) ;
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResultOk() {
		return setResponseResult(null, 200, true, "ok", null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(String msg) {
		return setResponseResult(null, 200, true, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(boolean status, String msg) {
		return setResponseResult(null, 200, status, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(String msg, Object data) {
		return setResponseResult(null, 200, true, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(boolean status, String msg, Object data) {
		return setResponseResult(null, 200, status, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(String msg, Object data, Object attrs) {
		return setResponseResult(null, 200, true, msg, data, attrs) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(boolean status, String msg, Object data, Object attrs) {
		return setResponseResult(null, 200, status, msg, data, attrs) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(HttpStatus httpStatus) {
		return setResponseResult(httpStatus, null, true, null, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(HttpStatus httpStatus, String msg) {
		return setResponseResult(httpStatus, null, true, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(HttpStatus httpStatus, String msg, Object data) {
		return setResponseResult(httpStatus, null, true, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultOk(HttpStatus httpStatus, String msg, Object data, Object attrs) {
		return setResponseResult(httpStatus, null, true, msg, data, attrs) ;
	}

	protected ResponseEntity<Map<String, Object>> setResponseResultError() {
		return setResponseResult(HttpStatus.INTERNAL_SERVER_ERROR, null, false, null, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(String msg) {
		return setResponseResult(HttpStatus.INTERNAL_SERVER_ERROR, null, false, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(String msg, Object data) {
		return setResponseResult(HttpStatus.INTERNAL_SERVER_ERROR, null, false, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(String msg, Object data, Object attrs) {
		return setResponseResult(HttpStatus.INTERNAL_SERVER_ERROR, null, false, msg, data, attrs) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(HttpStatus httpStatus) {
		return setResponseResult(httpStatus, null, false, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(HttpStatus httpStatus, String msg) {
		return setResponseResult(httpStatus, null, false, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(HttpStatus httpStatus, String msg, Object data) {
		return setResponseResult(httpStatus, null, false, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResultError(HttpStatus httpStatus, String msg, Object data, Object attrs) {
		return setResponseResult(httpStatus, null, false, msg, data, attrs) ;
	}

	protected ResponseEntity<Map<String, Object>> setResponseResult(HttpStatus httpStatus, boolean status, String msg) {
		return setResponseResult(httpStatus, httpStatus.value(), status, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResult(HttpStatus httpStatus, String msg, boolean status, Object data) {
		return setResponseResult(httpStatus, httpStatus.value(), status, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResult(HttpStatus httpStatus, String msg, boolean status, Object data, Object attrs) {
		return setResponseResult(httpStatus, httpStatus.value(), status, msg, data, attrs) ;
	}
	
	protected ResponseEntity<Map<String, Object>> setResponseResult(Integer code, boolean status, String msg) {
		return setResponseResult(null, status, msg, null, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResult(Integer code, boolean status, String msg, Object data) {
		return setResponseResult(null, code, status, msg, data, null) ;
	}
	protected ResponseEntity<Map<String, Object>> setResponseResult(Integer code, boolean status, String msg, Object data, Object attrs) {
		return setResponseResult(null, code, status, msg, data, attrs) ;
	}
	
	/**
	 * 客户端响应
	 * @param httpStatus 设置浏览器HTTP状态代码
	 * @param code 响应状态
	 * @param msg 响应信息
	 * @param data 响应数据
	 * @param attrs 附件属性数据
	 * @return
	 */
	protected ResponseEntity<Map<String, Object>> setResponseResult(HttpStatus httpStatus, Integer code, boolean status, String msg, Object data, Object attrs) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		if (null != data) {
			map.put("data", data);
		}
		if (null != attrs) {
			map.put("attrs", attrs);
		}
		if(null == httpStatus) {
			httpStatus = HttpStatus.OK ;
		}
		if(null == code) {
			code = httpStatus.value() ;
		}
		if(StringUtils.isBlank(msg)) {
			msg = httpStatus.name();
		}

		map.put("code", code);
		map.put("status", status);
		map.put("msg", msg);
		map.put("timestamp", System.currentTimeMillis());

		return new ResponseEntity<Map<String, Object>>(map, httpStatus);
	}

	/**
	 * 获取Controller注解上的路径
	 * 
	 * @return
	 */
	protected String defaultViewPrefix() {
		String currentViewPrefix = "";
		RequestMapping requestMapping = AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
		if (requestMapping != null && requestMapping.value().length > 0) {
			currentViewPrefix = requestMapping.value()[0];
		}
		return currentViewPrefix;
	}

	/**
	 * 设置视图名称
	 * 
	 * @param suffixName
	 * @return
	 */
	public String viewName(String suffixName) {
		if (!suffixName.startsWith("/")) {
			suffixName = "/" + suffixName;
		}
		return getViewPrefix() + suffixName;
	}

	/**
	 * 设置视图前缀
	 * 
	 * @param viewPrefix
	 */
	public void setViewPrefix(String viewPrefix) {
		if (viewPrefix.startsWith("/")) {
			viewPrefix = viewPrefix.substring(1);
		}
		this.viewPrefix = viewPrefix;
	}

	public String getViewPrefix() {
		return viewPrefix;
	}

}
